package innreservations;

/**
 *
 * @author Ethan
 */
public class PlaceReservation extends javax.swing.JFrame {
    
    private String start_date;
    private String end_date;
    private String room_name;
    private String rate;
    
    private boolean violate_max_occupancy = false;

    /**
     * Creates new form PlaceReservation
     */
    public PlaceReservation(String start_date, String end_date, String room_name, String rate) {
        initComponents();
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        
        this.start_date = start_date;
        this.end_date = end_date;
        this.room_name = room_name;
        this.rate = rate;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        FirstNameTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        LastNameTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        DiscountsSelection = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        AdultsSelection = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        ChildrenSelection = new javax.swing.JComboBox();
        MaxOccupancyViolated = new javax.swing.JLabel();
        MaxOccupancyViolated.setVisible(false);
        PlaceReservation = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        ReservationInfoTextArea = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Place a reservation:");

        jLabel2.setText("First Name:");

        jLabel3.setText("Last Name:");

        jLabel4.setText("Discounts:");

        DiscountsSelection.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "None", "AAA", "AARP" }));

        jLabel5.setText("Adults:");

        AdultsSelection.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4" }));
        AdultsSelection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AdultsSelectionActionPerformed(evt);
            }
        });

        jLabel6.setText("Children:");

        ChildrenSelection.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "3" }));
        ChildrenSelection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChildrenSelectionActionPerformed(evt);
            }
        });

        MaxOccupancyViolated.setText("Max Occupancy Violated");

        PlaceReservation.setText("Place a Reservation");
        PlaceReservation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PlaceReservationActionPerformed(evt);
            }
        });

        ReservationInfoTextArea.setColumns(20);
        ReservationInfoTextArea.setRows(5);
        jScrollPane1.setViewportView(ReservationInfoTextArea);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                                    .add(jLabel3)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(LastNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 135, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .add(jLabel1)
                                .add(jPanel1Layout.createSequentialGroup()
                                    .add(jLabel2)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(FirstNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 135, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel4)
                                    .add(jLabel5)
                                    .add(jLabel6))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jPanel1Layout.createSequentialGroup()
                                        .add(ChildrenSelection, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(MaxOccupancyViolated))
                                    .add(AdultsSelection, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(DiscountsSelection, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(PlaceReservation))
                        .add(0, 84, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(FirstNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(LastNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(DiscountsSelection, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(AdultsSelection, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(ChildrenSelection, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(MaxOccupancyViolated))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(PlaceReservation)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 152, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void PlaceReservationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PlaceReservationActionPerformed
        String first_name = FirstNameTextField.getText();
        String last_name = LastNameTextField.getText();
        String discounts = (String) DiscountsSelection.getSelectedItem();
        String adults = (String) AdultsSelection.getSelectedItem();
        String children = (String) ChildrenSelection.getSelectedItem();
        
        if(!first_name.isEmpty() && !last_name.isEmpty() & !violate_max_occupancy) {
            if(discounts.equals("AAA")) {
                rate = String.valueOf(.9 * Double.parseDouble(rate));
            } else if (discounts.equals("AARP")) {
                rate = String.valueOf(.85 * Double.parseDouble(rate));
            }
            
            InnReservations.create_reservation(String.valueOf(InnReservationUI.res_code), 
                    this.room_name, this.start_date, this.end_date, rate, last_name, 
                    first_name, adults, children);
            String reservation_info = "Your reservation is complete!\n\n";
            reservation_info += InnReservations.get_reservation_info(String.valueOf(InnReservationUI.res_code));
            
            ReservationInfoTextArea.setText(reservation_info);
            InnReservationUI.res_code++;
            InnReservations.reservations_count++;
            
            InnReservations.update_status();
            
            PlaceReservation.setVisible(false);
        }
    }//GEN-LAST:event_PlaceReservationActionPerformed

    private void AdultsSelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AdultsSelectionActionPerformed
        String adults = (String) AdultsSelection.getSelectedItem();
        String kids = (String) ChildrenSelection.getSelectedItem();
        int adults_selected = Integer.parseInt(adults);
        int kids_selected = Integer.parseInt(kids);
        int max_occupancy = InnReservations.get_max_occupancy(room_name);
        
        if(adults_selected + kids_selected > max_occupancy) {
            violate_max_occupancy = true;
            MaxOccupancyViolated.setVisible(true);
        } else {
            violate_max_occupancy = false;
            MaxOccupancyViolated.setVisible(false);
        }
    }//GEN-LAST:event_AdultsSelectionActionPerformed

    private void ChildrenSelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChildrenSelectionActionPerformed
        String adults = (String) AdultsSelection.getSelectedItem();
        String kids = (String) ChildrenSelection.getSelectedItem();
        int adults_selected = Integer.parseInt(adults);
        int kids_selected = Integer.parseInt(kids);
        int max_occupancy = InnReservations.get_max_occupancy(room_name);
        
        if(adults_selected + kids_selected > max_occupancy) {
            violate_max_occupancy = true;
            MaxOccupancyViolated.setVisible(true);
        } else {
            violate_max_occupancy = false;
            MaxOccupancyViolated.setVisible(false);
        }
    }//GEN-LAST:event_ChildrenSelectionActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox AdultsSelection;
    private javax.swing.JComboBox ChildrenSelection;
    private javax.swing.JComboBox DiscountsSelection;
    private javax.swing.JTextField FirstNameTextField;
    private javax.swing.JTextField LastNameTextField;
    private javax.swing.JLabel MaxOccupancyViolated;
    private javax.swing.JButton PlaceReservation;
    private javax.swing.JTextArea ReservationInfoTextArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
